<template>
  <div class="dictionary-data-container">
    <!-- 顶部筛选条件 -->
    <div class="conditions-container flex">
      <ScreenConditions :isDataType="false">
        <template slot="conditions">
          <!-- @change="conditionChange($event, 'status')" -->
          <!-- 角色名称 -->
          <bjSelect
            :dataResource="conditions.dictionarySource"
            placeholder="请选择字典名称"
            v-model="conditions.dictionary"
            :loading="conditions.dictionaryLoading"
            :replaceFields="{ key: 'dictTypeKey', label: 'dictTypeDesc' }"
            @change="dictionaryChange"
          ></bjSelect>
          <!-- 字典标签 -->
          <a-input
            v-model.trim="conditions.dicDesc"
            placeholder="请输入字典标签"
          />
        </template>
        <template slot="operate">
          <a-button type="primary" @click="getSearch"> 查询 </a-button>
        </template>
      </ScreenConditions>
      <a-button type="default" class="back-button" @click="back()">
        返回
      </a-button>
    </div>

    <!-- 主体区域 -->
    <div class="result-container">
      <div class="common-result-container-header" header="字典数据">
        <div>
          <a-button type="primary" @click="gotoAdd" v-if="showButton">
            新增
          </a-button>
          <a-button class="solid-line" @click="exportExcel" v-if="showButton">
            批量导出
          </a-button>
        </div>
      </div>
      <div class="result-body">
        <!-- table -->
        <div class="table-container" v-loading="table.loading">
          <vxe-table
            align="center"
            :data="table.source"
            show-overflow
            show-header-overflow
            show-footer-overflow
            show-footer
            :column-config="{ resizable: true }"
            class="scrollbarStyle"
            height="99%"
            :sort-config="{ trigger: 'cell' }"
            ref="xTable"
            :scroll-x="{ gt: 10 }"
            :scroll-y="{ gt: 100 }"
            row-id="id"
            :checkbox-config="{
              reserve: true,
            }"
          >
            <vxe-column
              :title="item.title"
              :field="item.key"
              :key="index"
              :min-width="item.width"
              show-overflow
              v-for="(item, index) in table.columns"
            >
            </vxe-column>
            <vxe-column title="操作" width="180" fixed="right">
              <template #default="{ row }">
                <div class="text-button-group">
                  <a-button
                    type="link"
                    @click="gotoEdit(row)"
                    v-if="showButton"
                  >
                    编辑
                  </a-button>
                  <a-button
                    type="link"
                    class="danger"
                    @click="handleDelClick(row)"
                    v-if="showButton"
                  >
                    删除
                  </a-button>
                </div>
              </template>
            </vxe-column>
          </vxe-table>
        </div>
        <!-- 分页 -->
        <div class="pager-container">
          <vxe-pager
            background
            :current-page.sync="pager.page"
            :page-size.sync="pager.pageSize"
            :total="pager.total"
            :layouts="[
              'PrevPage',
              'JumpNumber',
              'NextPage',
              'Sizes',
              'FullJump',
            ]"
            @page-change="paginationChange"
          >
            <template #left>
              <div class="vxe-pager-left-tip-container">
                第<span>1</span>到<span
                  >{{ Math.ceil(pager.total / pager.pageSize) || 1 }}页 </span
                >共<span>{{ pager.total }}</span
                >条记录
              </div>
            </template>
          </vxe-pager>
        </div>
      </div>
    </div>
    <!-- 新增、编辑弹窗 -->
    <dicAddModal :visible.sync="addVisible" :model="model"></dicAddModal>
  </div>
</template>

<script>
//【字典数据】列表
import bjSelect from "@/components/bj-select";
import dicAddModal from "./components/dicAddModal";
import { notificationTip, downLoadFileByArraybuffer } from "@/utils";
import {
  getDictManageList,
  getDictTypeSelectList,
  getDictDelete,
  exportDict,
} from "@/api/user-center/dictionaryManage.js";
export default {
  components: {
    bjSelect,
    dicAddModal,
  },
  data() {
    return {
      //顶部筛选条件
      conditions: {
        //字典名称
        dictionarySource: [],
        dictionary: undefined,
        dictionaryLoading: false,
        //字典标签
        dicDesc: undefined,
      },
      //table
      table: {
        loading: false,
        source: [],
        columns: [
          {
            title: "字典类别",
            dataIndex: "dictTypeDesc",
            key: "dictTypeDesc",
            width: "",
          },
          {
            title: "字典标签",
            dataIndex: "dicDesc",
            key: "dicDesc",
            width: "",
          },
          {
            title: "字典键值",
            dataIndex: "dicValue",
            key: "dicValue",
            width: "",
          },
          {
            title: "排序",
            dataIndex: "sort",
            key: "sort",
            width: "",
          },
          {
            title: "备注",
            dataIndex: "remark",
            key: "remark",
            width: "",
          },
          {
            title: "创建时间",
            dataIndex: "createTime",
            key: "createTime",
            width: "",
          },
        ],
      },
      //分页
      pager: {
        page: 1,
        pageSize: 10,
        total: 0,
      },
      //新增编辑弹窗
      addVisible: false,
      model: {
        id: "",
        text: "",
      },
      //按钮权限
      showButton: false,
    };
  },
  mounted() {
    let { dictTypeKey, dictTypeDesc, showButton } = this.$route.query;
    this.showButton = showButton;
    this.conditions.dictionary = {
      key: dictTypeKey,
      lable: dictTypeDesc,
    };
    this.getSelectData();
    this.getTableData();
  },
  methods: {
    //查询字典下拉框
    getSelectData() {
      this.conditions.dictionaryLoading = true;
      getDictTypeSelectList()
        .then((res) => {
          this.conditions.dictionarySource = res?.data?.result || [];
        })
        .catch((_) => {
          this.conditions.dictionarySource = [];
        })
        .finally((_) => {
          this.conditions.dictionaryLoading = false;
        });
    },
    //选择字典
    dictionaryChange(ev) {
      this.conditions.dictionary = ev;
      this.getSearch();
    },
    //查询列表
    getTableData() {
      this.table.loading = true;
      let { page, pageSize } = this.pager;
      let { dictionary = {}, dicDesc = "" } = this.conditions;
      return new Promise((resolve, reject) => {
        getDictManageList({
          dictKey: dictionary?.key,
          dicDesc,
          page,
          pageSize,
        })
          .then((res) => {
            let {
              data: { result },
            } = res;
            this.table.source = result?.rows || [];
            this.pager.total = result?.total || 0;
            resolve();
          })
          .catch((_) => {
            this.table.source = [];
            this.pager.total = 0;
            reject();
          })
          .finally((_) => {
            this.table.loading = false;
          });
      });
    },
    //查询
    getSearch() {
      this.pager.page = 1;
      this.getTableData();
    },
    //新增
    gotoAdd() {
      this.model.text = "新增";
      this.model.id = "";
      this.addVisible = true;
    },
    //编辑
    gotoEdit(row) {
      this.addVisible = true;
      this.model = {
        text: "编辑",
        id: row.id,
        //字典名称
        dicDesc: row.dicDesc,
        //字典类型
        dictKey: row.dictKey,
        //排序
        sort: row.sort,
        //备注
        remark: row.remark,
      };
    },
    //批量导出
    exportExcel() {
      let { dictionary = {}, dicDesc = "" } = this.conditions;
      exportDict({ dictKey: dictionary?.key, dicDesc })
        .then((data) => {
          downLoadFileByArraybuffer(data, "字典数据");
        })
        .catch();
    },
    //删除
    handleDelClick(row) {
      let that = this;
      let dataLength = that.table.source.length;
      let { id } = row;
      const h = this.$createElement;
      this.$confirm({
        title: "您确定删除所选内容?",
        content: "删除内容后,将无法恢复。",
        okText: "确定",
        okType: "primary",
        cancelText: "取消",
        centered: true,
        icon: () =>
          h("a-icon", {
            props: {
              type: "question-circle",
              theme: "filled",
            },
            style: "color:#FB5A5A",
          }),
        onOk() {
          getDictDelete(id)
            .then((res) => {
              if (dataLength == 1 && that.pager.page > 1) {
                that.pager.page -= 1;
              }
              notificationTip("success", "删除字典数据成功！");
              that.getTableData();
            })
            .catch();
        },
        onCancel() {},
      });
    },

    //监听分页切换
    paginationChange({ currentPage, pageSize }) {
      this.pager.page = currentPage;
      this.pager.pageSize = pageSize;
      this.getTableData();
    },

    //返回
    back() {
      this.$router.back();
    },
  },
};
</script>

<style lang="less" scoped>
.dictionary-data-container {
  height: 100%;
  .flex();
  flex-direction: column;
  box-sizing: border-box;
  padding: 0 0 10px 10px;
  .conditions-container {
    flex-shrink: 0;
    background: #fff;
    box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.14);
    align-items: center;
    .conditions-screening-container {
      flex: 1;
      box-shadow: none;
    }
    .back-button {
      margin: 0 20px;
    }
  }
  .result-container {
    .flex();
    flex-direction: column;
    flex: 1;
    .common-result-container-header {
      flex-shrink: 0;
    }
    .result-body {
      flex: 1;
      .flex();
      flex-direction: column;
      .table-container {
        flex: 1;
        .table-more-btns {
          color: var(--primary-color);
          margin-top: -20px;
          transform: translateY(-3px);
          margin-left: 20px;
          display: inline-block;
          cursor: pointer;
        }
        .status-text {
          background: #4bc964;
          border-radius: 4px;
          color: #fff;
          padding: 4px 15px;
          &.error {
            background: #ff3f4a;
          }
        }
      }
      .pager-container {
        flex-shrink: 0;
      }
    }
  }
}
</style>
